/**
 * 
 */
package com.facebookdirectory.session;

import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityQuery;

import com.facebookdirectory.entity.FBAppInfo;

/**
 * @author piglet
 *
 */
@Name("appInfoList")
public class AppInfoList extends EntityQuery<FBAppInfo>{
	private static final long serialVersionUID = -2192064477701968615L;
	private static final int PAGE_COUNT = 9;
	
	public AppInfoList() {	
		setMaxResults(15);
	}
	/**
	 * @return the page
	 */
	public int getPage() {
		Integer maxResult = getMaxResults();
		if (maxResult != null && maxResult != 0 && getFirstResult() != null) {
			return getFirstResult() / getMaxResults() + 1;
		}

		return 1;
	}
	
	/**
	 * Returns true if the previous page exists
	 */
	public boolean isPreviousPageExists() {
		return getPage() > 1;
	}

	/**
	 * Get the index of the first result of the previous page
	 * 
	 */
	public int getPreviousPage() {
		return (getPage() > 1) ? getPage() - 1 : 1;

	}

	public boolean isNextPageExists() {
		return getPage() < getPageCount();
	}

	/**
	 * Get the index of the first result of the next page
	 * 
	 */
	public int getNextPage() {
		return (getPage() < getPageCount()) ? getPage() + 1 : getPageCount();
	}

	public List<Integer> getPageList() {
		Integer i = 0;
		List<Integer> pages = new ArrayList<Integer>();
		int pageCount = getPageCount();
		int page = getPage();
		int end = page + PAGE_COUNT <= pageCount ? page + PAGE_COUNT
				: pageCount;
		int begin = end - 2 * PAGE_COUNT > 0 ? end - 2 * PAGE_COUNT : 1;

		for (i = begin; i <= end; i++) {
			pages.add(i);
		}
		return pages;
	}
	
}
